package com.sailing.xjpb.ipaas.client.core.impl;

import com.sailing.xjpb.common.EJBGenerator;
import com.sailing.xjpb.common.SailingAssert;
import com.sailing.xjpb.common.exception.BusinessException;
import com.sailing.xjpb.ipaas.client.core.Sip3Service;
import com.sailing.xjpb.ipaas.client.core.repository.SipOrganizationRepository;
import com.sailing.xjpb.ipaas.client.core.repository.SipRoleRepository;
import com.sailing.xjpb.ipaas.client.core.repository.SipUserInfoRepository;
import com.sailing.xjpb.ipaas.client.entity.bo.OrganizationTreeBO;
import com.sailing.xjpb.ipaas.client.entity.bo.SipOrganizationBO;
import com.sailing.xjpb.ipaas.client.entity.bo.SipRoleBO;
import com.sailing.xjpb.ipaas.client.entity.bo.SipUserInfoBO;
import com.sailing.xjpb.ipaas.client.entity.dto.SipOrganizationDTO;
import com.sailing.xjpb.ipaas.client.entity.dto.SipRoleDTO;
import com.sailing.xjpb.ipaas.client.entity.dto.SipUserInfoDTO;
import com.sailing.xjpb.ipaas.client.entity.dto.SipUserOrgDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户/组织相关业务接口实现
 */
public class Sip3ServiceImpl implements Sip3Service {

    @Autowired
    private SipOrganizationRepository sipOrganizationRepository;

    @Autowired
    private SipUserInfoRepository sipUserInfoRepository;

    @Autowired
    private SipRoleRepository sipRoleRepository;

    @Override
    public SipUserInfoBO querySipUserInfoBySipUserId(String userId) {
        SipUserInfoDTO sipUserInfoDTO = sipUserInfoRepository.findUserByUserId(userId);
        return EJBGenerator.convert(sipUserInfoDTO, SipUserInfoBO.class);
    }

    @Override
    public String getUserNameById(String userId) {
        if (StringUtils.isBlank(userId)) {
            return "";
        }
        SipUserInfoDTO sipUserInfoDTO = sipUserInfoRepository.findUserByUserId(userId);
        return sipUserInfoDTO == null ? userId : sipUserInfoDTO.getName();
    }

    @Override
    public SipOrganizationBO querySipOrganizationByOrgCode(String orgCode) {
        SipOrganizationDTO sipOrganizationDTO = sipOrganizationRepository.findByOrgCode(orgCode);
        return EJBGenerator.convert(sipOrganizationDTO, SipOrganizationBO.class);
    }

    @Override
    public SipOrganizationBO querySipOrganizationByOrgCodeV1(String orgCode) {
        SipOrganizationDTO sipOrganizationDTO = sipOrganizationRepository.findByOrgCodeV1(orgCode);
        return EJBGenerator.convert(sipOrganizationDTO, SipOrganizationBO.class);
    }

    @Override
    public SipOrganizationBO querySipOrganizationByOrgId(String orgId) {
        SipOrganizationDTO sipOrganizationDTO = sipOrganizationRepository.findByOrgId(orgId);
        return EJBGenerator.convert(sipOrganizationDTO, SipOrganizationBO.class);
    }

    @Override
    public List<SipOrganizationBO> querySipOrganizationListByPId(String pId) {
        List<SipOrganizationDTO> sipOrganizationDTOList = sipOrganizationRepository.findByParentId(pId);
        return EJBGenerator.convert(sipOrganizationDTOList, SipOrganizationBO.class);
    }

    @Override
    public List<SipOrganizationBO> findConSelfByOrgId(String orgId) {
        List<SipOrganizationDTO> sipOrganizationDTOList = sipOrganizationRepository.findConSelfByOrgId(orgId);
        return EJBGenerator.convert(sipOrganizationDTOList, SipOrganizationBO.class);
    }

    @Override
    public List<String> findOrgCodeListByOrgId(String id) {
        if (StringUtils.isBlank(id)) {
            return Collections.emptyList();
        }
        List<SipOrganizationBO> sipOrganizationBOList = this.findConSelfByOrgId(id);
        if (!CollectionUtils.isEmpty(sipOrganizationBOList)) {
            return sipOrganizationBOList.stream().map(SipOrganizationBO::getOrgCode).collect(Collectors.toList());
        }
        throw BusinessException.businessError("组织ID无法通过SYS_ORG_CODE查到数据：" + id);
    }

    @Override
    public List<SipOrganizationBO> findByLevel(String level) {
        List<SipOrganizationDTO> sipOrganizationDTOList = sipOrganizationRepository.findOrgByLevel(level);
        return EJBGenerator.convert(sipOrganizationDTOList, SipOrganizationBO.class);
    }

    @Override
    public List<SipOrganizationBO> findAll() {
        List<SipOrganizationDTO> sipOrganizationDTOList = sipOrganizationRepository.findAll();
        return EJBGenerator.convert(sipOrganizationDTOList, SipOrganizationBO.class);
    }

    /**
     * 根据组织机构orgId获得上级组织机构列表
     */
    @Override
    public List<SipOrganizationBO> findParentOrgList(String orgId) {
        SailingAssert.hasText(orgId, "组织机构ID不能为空");
        List<SipOrganizationDTO> findAllParentOrg = sipOrganizationRepository.findAllParentOrg(orgId);
        return EJBGenerator.convert(findAllParentOrg, SipOrganizationBO.class);
    }

    @Override
    public String getOrgNameById(String orgId) {
        if (StringUtils.isBlank(orgId)) {
            return null;
        }
        SipOrganizationBO organizationBO = this.querySipOrganizationByOrgId(orgId);
        return organizationBO == null ? orgId : organizationBO.getShortName();
    }

    @Override
    public List<OrganizationTreeBO> findOrganizationTreeByOrgId(String id) {
        List<SipOrganizationDTO> sipOrganizationDtoList = sipOrganizationRepository.findConSelfByOrgId(id);
        return EJBGenerator.convert(sipOrganizationDtoList, OrganizationTreeBO.class);
    }

    @Override
    public List<OrganizationTreeBO> findOrganizationTreeByOrgCode(String orgCode) {
        SipOrganizationDTO org = sipOrganizationRepository.findByOrgCode(orgCode);
        List<SipOrganizationDTO> sipOrganizationDtoList = sipOrganizationRepository.findConSelfByOrgId(org.getOrgId());
        return EJBGenerator.convert(sipOrganizationDtoList, OrganizationTreeBO.class);
    }

    @Override
    public SipRoleBO findByRoleCode(String roleCode) {
        SipRoleDTO sipRoleDTO = sipRoleRepository.findByRoleCode(roleCode);
        return EJBGenerator.convert(sipRoleDTO, SipRoleBO.class);
    }

    @Override
    public List<SipUserInfoBO> findChildAllUser(String orgId) {
        List<SipUserInfoDTO> userIdByOrgId = sipUserInfoRepository.findChildAllUser(orgId);
        return EJBGenerator.convert(userIdByOrgId, SipUserInfoBO.class);
    }

    @Override
    public List<SipUserInfoBO> findUserByOrgId(String orgId) {
        List<SipUserInfoDTO> userIdByOrgId = sipUserInfoRepository.findUserByOrgId(orgId);
        return EJBGenerator.convert(userIdByOrgId, SipUserInfoBO.class);
    }

    @Override
    public List<SipUserOrgDTO> listRoleUsers(String authorization, String orgCode, String orgMode, String roleCode) {
        return sipUserInfoRepository.listRoleUsers(authorization, orgCode, orgMode, roleCode);
    }

}
